package com.adnaan.backend.controller.wechat;

import cn.dev33.satoken.stp.StpUtil;
import com.adnaan.backend.common.annotation.RestControllerMapping;
import com.adnaan.backend.common.model.Result;
import com.adnaan.backend.common.model.UserInfo;
import com.adnaan.backend.entity.SysUser;
import com.adnaan.backend.entity.UserCollect;
import com.adnaan.backend.entity.ViewHistory;
import com.adnaan.backend.service.ArticleService;
import com.adnaan.backend.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

/**
 * author: adnaan
 * create: 2025-02-24 12:44
 * Description:
 */
@RestControllerMapping("/mine")
public class MineController {

    @Resource
    private ArticleService articleService;

    @Resource
    private SysUserService sysUserService;

    /**
     * 统计数据
     */
    @GetMapping("/statistics")
    public Result<Map<String, Long>> statistics() {
        Long userId = StpUtil.getLoginIdAsLong();
        Map<String, Long> map = articleService.statistics(userId);
        return Result.success(map);
    }

    /**
     * 获取我的收藏
     */
    @GetMapping("/collect")
    public Result<Page<UserCollect>> pageQuery(@RequestParam Integer pageNo, @RequestParam Integer pageSize) {
        Page<UserCollect> page = new Page<>(pageNo, pageSize);
        Long userId = StpUtil.getLoginIdAsLong();
        page.setTotal(articleService.collectCount(userId));
        page.setCurrent(pageNo);
        page.setPages(page.getSize() * page.getCurrent());
        page.setRecords(articleService.collectPage(page, userId));
        page.getRecords().forEach(item -> {
            Map<String, String> content = articleService.getContent(item.getArticleId());
            item.setTitle(content.get("title"));
            item.setCover(content.get("cover"));
            item.setDescription(content.get("description"));
        });
        return Result.success(page);
    }

    /**
     * 获取浏览历史
     */
    @GetMapping("/viewHistory")
    public Result<Page<ViewHistory>> page(@RequestParam Integer pageNo, @RequestParam Integer pageSize) {
        Page<ViewHistory> page = new Page<>(pageNo, pageSize);
        Long userId = StpUtil.getLoginIdAsLong();
        page.setTotal(articleService.viewHistoryCount(userId));
        page.setCurrent(pageNo);
        page.setPages(page.getSize() * page.getCurrent());
        page.setRecords(articleService.viewHistoryPage(page, userId));
        page.getRecords().forEach(item -> {
            Map<String, String> content = articleService.getContent(item.getArticleId());
            item.setTitle(content.get("title"));
            item.setCover(content.get("cover"));
        });
        return Result.success(page);
    }

    /**
     * 清空浏览历史
     */
    @GetMapping("/clearViewHistory")
    public Result<String> clearViewHistory() {
        Long userId = StpUtil.getLoginIdAsLong();
        if (articleService.clearViewHistory(userId)) {
            return Result.success("清空成功");
        }
        return Result.error("清空失败");
    }

    /**
     * 个人信息设置
     */
    @PostMapping("/setInfo")
    public Result<Boolean> setInfo(@RequestBody UserInfo userInfoVo) {
        LambdaUpdateWrapper<SysUser> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(SysUser::getId, StpUtil.getLoginIdAsLong());
        wrapper.set(SysUser::getNickname, userInfoVo.getNickName());
        wrapper.set(SysUser::getAvatar, userInfoVo.getAvatar());
        wrapper.set(SysUser::getSex, userInfoVo.getSex());
        wrapper.set(SysUser::getEmail, userInfoVo.getEmail());
        wrapper.set(SysUser::getPhone, userInfoVo.getPhone());
        return Result.success(sysUserService.update(wrapper));
    }

    /**
     * 获取个人信息
     */
    @GetMapping("/getInfo")
    public Result<UserInfo> getInfo() {
        Long userId = StpUtil.getLoginIdAsLong();
        SysUser sysUser = sysUserService.getById(userId);
        UserInfo userInfoVo = new UserInfo();
        userInfoVo.setNickName(sysUser.getNickname());
        userInfoVo.setAvatar(sysUser.getAvatar());
        userInfoVo.setSex(sysUser.getSex());
        userInfoVo.setEmail(sysUser.getEmail());
        userInfoVo.setPhone(sysUser.getPhone());
        return Result.success(userInfoVo);
    }
}
